<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

global $dal;

$focus = getPostValue( 'focus' );
$coal = getPostValue( 'coal' );
$iron = getPostValue( 'iron' );
$food = getPostValue( 'farming' );
$villageId = getPostValue( 'village_id' );
$automatic = getPostValue( 'automatic' );
$sendWorkersTo = getPostValue( 'transfer_to' );
$sendWorkersAmount = getPostValue( 'transfer_number' );
if( !isset( $sendWorkersTo ) || !is_numeric( $sendWorkersTo ) ) {
	$sendWorkersTo = -1;
}
if( !isset( $sendWorkersAmount ) || !is_numeric( $sendWorkersAmount ) ) {
	$sendWorkersAmount = 0;
}

if( isset( $automatic ) && $automatic == 'automatic' ) {
	$automatic = 1;
}
else {
	$automatic = 0;
}

$statement = $dal->prepare( 'SELECT name ' .
		'FROM village ' .
		'WHERE ruler_id = :ruler_id AND village_id = :village_id' );
$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
$statement->bindParam( ':village_id', $villageId, PDO::PARAM_INT );
$name = $dal->getValue( $statement );
if( isset( $name ) ) {
	if( $focus != 'housing' && $focus != 'farming' && $focus != 'iron' && $focus != 'coal' ) {
		$focus = 'no';
	}
	
	if( !is_numeric( $coal ) ) {
		$coal = 0;
	}
	if( !is_numeric( $iron ) ) {
		$iron = 0;
	}
	if( !is_numeric( $food ) ) {
		$food = 0;
	}
	
	$sendWorkersAmount = ( int ) $sendWorkersAmount;
	if( $sendWorkersAmount < 1 ) {
		$sendWorkersAmount = 0;
	}
	
	if( $sendWorkersAmount != 0 ) {
		$statement = $dal->prepare( 'SELECT count(*) ' .
				'FROM village ' .
				'WHERE ruler_id = :ruler_id AND village_id = :village_id' );
		$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
		$statement->bindParam( ':village_id', $sendWorkersTo, PDO::PARAM_INT );
		if( $dal->getValue( $statement ) > 0 ) {
			$statement = $dal->prepare( 'SELECT workforce, jobs, send_workers_amount ' .
					'FROM village ' .
					'WHERE village_id = :village_id' );
			$statement->bindParam( ':village_id', $villageId, PDO::PARAM_INT );
			$unemploymentVariables = $dal->getObject( $statement );
			$unemployed = ( $unemploymentVariables->workforce - $unemploymentVariables->jobs ) + $unemploymentVariables->send_workers_amount;
			
			if( $sendWorkersAmount > $unemployed ) {
				$sendWorkersAmount = $unemployed;
			}
			if( $unemploymentVariables->send_workers_amount == $sendWorkersAmount ) {
				$jobsOccupied = $unemploymentVariables->jobs;
			}
			else {
				$jobsOccupied = $unemploymentVariables->jobs + ( $sendWorkersAmount - $unemploymentVariables->send_workers_amount );
			}
		}
		else {
			$sendWorkersTo = -1;
		}
	}
	else {
		$statement = $dal->prepare( 'SELECT jobs ' .
				'FROM village ' .
				'WHERE village_id = :village_id' );
		$statement->bindParam( ':village_id', $villageId, PDO::PARAM_INT );
		$jobsOccupied = $dal->getValue( $statement );
	}
	
	$statement = $dal->prepare( 'UPDATE village ' .
			'SET food_jobs_prefered = :food_jobs_prefered, iron_jobs_prefered = :iron_jobs_prefered, ' .
				'coal_jobs_prefered = :coal_jobs_prefered, focus = :focus, automatic = :automatic, ' .
				'send_workers_to = :send_workers_to, send_workers_amount = :send_workers_amount, jobs = :jobs ' .
			'WHERE village_id = :village_id' );
	$statement->bindParam( ':food_jobs_prefered', $food, PDO::PARAM_INT );
	$statement->bindParam( ':iron_jobs_prefered', $iron, PDO::PARAM_INT );
	$statement->bindParam( ':coal_jobs_prefered', $coal, PDO::PARAM_INT );
	$statement->bindParam( ':focus', $focus, PDO::PARAM_STR );
	$statement->bindParam( ':automatic', $automatic, PDO::PARAM_INT );
	$statement->bindParam( ':send_workers_to', $sendWorkersTo, PDO::PARAM_INT );
	$statement->bindParam( ':send_workers_amount', $sendWorkersAmount, PDO::PARAM_INT );
	$statement->bindParam( ':jobs', $jobsOccupied, PDO::PARAM_INT );
	$statement->bindParam( ':village_id', $villageId, PDO::PARAM_INT );
	$dal->execute( $statement );
	header( 'Location: village.php?name=' . $name );
}
else {
	header( 'Location: villages.php' );
}
?>
